<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/3/12
 * Time: 17:32
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Sensitive extends Base
{
    /**
     * 美丽日记分类
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('sensitive')->count();// 获取总条数
            $lists = Db::name('sensitive')
                ->field('s_id, s_word, s_time, s_updatetime')
                ->order('s_time desc')
                ->page($Nowpage,$limits)
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加敏感词
     * @return mixed|\think\response\Json
     */
    public function sensitiveAdd(){
        if(Request::instance()->isPost()){
            $s_word = trim(input('s_word'), ' ');
            $isHave = Db::table('up_sensitive')->where('s_word', $s_word)->find();
            if($isHave){
                return json(['code' => 100, 'msg' => '该敏感词已存在']);
            }
            $insert = [
                's_word' => $s_word,
                's_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_sensitive')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('sensitive/sensitiveadd');
    }
    
    /**
     * 编辑敏感词
     * @return mixed|\think\response\Json
     */
    public function sensitiveEdit(){
        $s_id = input('s_id');
        if(Request::instance()->isPost()){
            $s_word = trim(input('s_word'), ' ');
            $isHave = Db::table('up_sensitive')
                ->where('s_word', $s_word)
                ->where('s_id', 'NEQ', $s_id)
                ->find();
            if($isHave){
                return json(['code' => 100, 'msg' => '该敏感词已存在']);
            }
            $res = Db::table('up_sensitive')->where('s_id', $s_id)
                ->update([
                    's_word' => $s_word,
                    's_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $s_word = Db::table('up_sensitive')->where('s_id', $s_id)->value('s_word');
        $this->assign([
            's_id' => $s_id,
            's_word' => $s_word
        ]);
        return $this->fetch('sensitive/sensitiveedit');
    }
    
    /**
     * 删除敏感词
     * @return \think\response\Json
     */
    public function sensitiveDel(){
        $s_id = input('id');
        $res = Db::table('up_sensitive')->delete($s_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}